package com.huawei.cloud.phone.management.domain.user;

import com.huawei.cloud.phone.management.common.utils.RandomStringUtils;
import com.huawei.cloud.phone.management.common.security.EncryptionServiceFactory;
import com.huawei.cloud.phone.management.common.utils.JsonUtils;
import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    static final long serialVersionUID = -7034897190745716121L;

    private String userId;

    private String username;

    private String password;

    private String sessionId;

    private String token;

    /**
     * 处理敏感信息，对敏感信息进行加密
     */
    public void handleSensitiveInfo() {
        this.password = EncryptionServiceFactory.getMd5Service().encrypt(this.password);
    }

    public void genLoginInfo() {
        this.password = null;
        this.sessionId = RandomStringUtils.randomUuidNoHyphens();
        this.token = EncryptionServiceFactory.getAesService().encrypt(JsonUtils.toJsonString(this));
    }

    public static String parseUserId(String token) {
        String decrypt = EncryptionServiceFactory.getAesService().decrypt(token);
        User user =  JsonUtils.parseData(decrypt, User.class);
        return user.getUserId();
    }
}
